/**
 * Created by Administrator on 2017/6/28.
 */
var viewer = null;
var status = 0;
$(function () {
    $("#jqGrid").jqGrid({
        url: '/sys/store/list',
        datatype: "json",
        mtype: "get",
        colModel: [{
            label: 'Id',
            name: 'id',
            index: 'id',
            width: 50,
            hidden: true,
            key: true
        }, {
            label: 'legalName',
            name: 'legalName',
            index: 'legalName',
            width: 50,
            hidden: true
        },
            {
                label: 'legalIdCard',
                name: 'legalIdCard',
                index: 'legalIdCard',
                width: 50,
                hidden: true
            },
            {
                label: 'idCardPos',
                name: 'idCardPos',
                index: 'idCardPos',
                width: 50,
                hidden: true
            },
            {
                label: 'idCardBack',
                name: 'idCardBack',
                index: 'idCardBack',
                width: 50,
                hidden: true
            }
            ,
            {
                label: '营业执照名称',
                name: 'licenseName',
                index: 'licenseName',
                width: 120,
                hidden: true
            },
            {
                label: 'licenseImg',
                name: 'licenseImg',
                index: 'licenseImg',
                width: 50,
                hidden: true
            },
            {
                label: 'holdIdCard',
                name: 'holdIdCard',
                index: 'holdIdCard',
                width: 50,
                hidden: true
            }
            ,
            {
                label: 'enterImg',
                name: 'enterImg',
                index: 'enterImg',
                width: 50,
                hidden: true
            },
            {
                label: '商户号',
                name: 'userId',
                index: 'userId',
                width: 80,
            },
            {
                label: '手机号',
                name: 'phone',
                index: 'phone',
                width: 100
            },
            {
                label: '商户名称',
                name: 'licenseName',
                index: 'licenseName',
                width: 100
            },
            {
                label: '商户简称',
                name: 'merAbbreviation',
                index: 'merAbbreviation',
                width: 100,
            },
            {
                label: '营业执照编号',
                name: 'licenseNumber',
                index: 'licenseNumber',
                width: 120
            },
            {
                label: '提交时间',
                name: 'createDate',
                index: 'createDate',
                width: 150
            },
            {
                label: '审核时间',
                name: 'updateDate',
                index: 'updateDate',
                width: 150
            },
            {
                label: '审核状态',
                name: 'status',
                index: 'status',
                width: 100,
                formatter: function (value, options, row) {
                    if (value === 0) return "待审核";
                    if (value === 1) return "审核通过";
                    if (value === 2) return "审核失败";
                }
            },
            {
                label: '审核描述',
                name: 'explains',
                index: 'explains',
                width: 100
            }

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "data.list",
            page: "data.currPage",
            total: "data.totalPage",
            records: "data.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({
                "overflow-x": "hidden"
            });
        }
    });
    $('input[name=inlineRadioOptions5]').click(function () {
        var selectedId = $(this).val();
        if (selectedId != 1) {
            vm.showCause = true;
        } else {
            vm.showCause = false;
        }
        status = selectedId;
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        showInfo: false,
        showCause: false,
        showHandle: true,
        dataInfo: {}
    },
    methods: {

        info: function () {
            var del = getSelectedRows();
            if (del.length == 0) {
                alert("请选中一条数据");
                return;
            } else if (del.length > 1) {
                alert("不能批量操作");
                return;
            }
            var rowid = $("#jqGrid").getGridParam("selrow");
            var rowData = $("#jqGrid").getRowData(rowid);
            vm.dataInfo = rowData;
            vm.showInfo = true;
            vm.showList = false;
            if (vm.dataInfo.status != "待审核") {
                vm.showHandle = false;
            } else {
                vm.showHandle = true;
            }
        },
        showImage: function (index) {
            if (viewer == null) {
                viewer = new Viewer(document.getElementById("images"), {
                    url: 'src',
                });
            }
            if (viewer != null) {
                viewer.view(index).show();
            }
        },
        query: function () {
            var searchContent = $("#searchContent").val();
            $.ajax({
                url: "/sys/store/query?phone=" + searchContent + "&page=1&limit=10",
                dataType: "JSON",
                type: "get",
                success: function (result) {
                    if (result.code == 0) {
                        $("#jqGrid").jqGrid('clearGridData'); //清空表格
                        $("#jqGrid").jqGrid('setGridParam', { // 重新加载数据
                            datatype: 'local',
                            data: result.data.list, //  newdata 是符合格式要求的需要重新加载的数据
                            page: 1
                        }).trigger("reloadGrid");
                    } else {
                        alert(result.msg);
                    }
                },
                error: function (e) {
                    console.log("error", e);
                    alert("请求失败，请重试");
                }
            });
        },
        commit: function () {
            if (status == 0) {
                alert("请选择审核操作");
            } else {
                var urlStr = status == 1 ? "/sys/store/pass" : "/sys/store/refuse";
                var cause = $("#causeContent").val();
                $.ajax({
                    url: urlStr + "?id=" + vm.dataInfo.id + "&explain=" + cause,
                    type: "get",
                    dataType: "JSON",
                    success: function (result) {
                        if (result.code == 0) {
                            alert("操作成功");
                            vm.reload();
                        } else {
                            alert(result.msg);
                        }
                    },
                    error: function (e) {
                        alert("请求失败，请重试");
                        console.log("error", e);
                    }
                })
            }
        },
        reload: function () {
            vm.showList = true;
            vm.showInfo = false;
            vm.dataInfo = {};
            vm.showHandle = true;
            vm.showCause = false;
            $("#jqGrid").jqGrid({
                url: '/sys/store/list',
                datatype: "json",
                mtype: "get"
            }).trigger("reloadGrid");
        }
    }
});